package com.cheyunkeji.er.view.auction;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cheyun.netsalev3.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/** Created by Michael on 2017/3/9 16:51. */
public class CarArchiveItemView extends RelativeLayout implements View.OnClickListener {

  @BindView(R.id.tv_item_index)
  TextView tvItemIndex;

  @BindView(R.id.tv_item_name)
  TextView tvItemName;

  @BindView(R.id.iv_item_status)
  ImageView ivItemStatus;

  @BindView(R.id.tv_problem_desc)
  TextView tvProblemDesc;

  @BindView(R.id.iv_problem_indicator)
  ImageView ivProblemIndicator;

  @BindView(R.id.content_view)
  RelativeLayout contentView;

  OnPhotoIconClickListener iconClickListener;
  private View rootView;
  private String txtIndexNumber, txtItemDesc;
  private String imgPath = null;

  public CarArchiveItemView(Context context) {
    super(context);
    initView(context);
  }

  public CarArchiveItemView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initView(context);

    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CarArchiveItemView);

    for (int i = 0; i < typedArray.getIndexCount(); i++) {

      switch (typedArray.getIndex(i)) {
        case R.styleable.CarArchiveItemView_txt_item_name:
          txtItemDesc = typedArray.getString(typedArray.getIndex(i));
          tvItemName.setText(txtItemDesc);
          break;
        case R.styleable.CarArchiveItemView_txt_number_index:
          txtIndexNumber = typedArray.getString(typedArray.getIndex(i));
          tvItemIndex.setText(txtIndexNumber);
          tvItemIndex.setVisibility(View.VISIBLE);
          break;
        case R.styleable.CarArchiveItemView_background_color:
          contentView.setBackgroundColor(
              typedArray.getColor(typedArray.getIndex(i), Color.parseColor("#00000000")));
        default:
          break;
      }
    }
    typedArray.recycle();
  }

  public CarArchiveItemView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initView(context);
  }

  private void initView(Context context) {
    rootView = LayoutInflater.from(context).inflate(R.layout.er_car_archive_item, this, true);
    ButterKnife.bind(this, rootView);

    ivProblemIndicator.setOnClickListener(this);
  }

  public void showProblemStatu(String problemDesc) {
    tvProblemDesc.setText(problemDesc);
    tvProblemDesc.setVisibility(View.VISIBLE);
    ivItemStatus.setImageResource(R.mipmap.er_car_archive_item_error);
  }

  public void showPhotoIcon(OnPhotoIconClickListener listener, String imgPath) {
    ivProblemIndicator.setVisibility(View.VISIBLE);
    this.iconClickListener = listener;
    this.imgPath = imgPath;
  }

  public void setItemDesc(String itemDesc) {
    tvItemName.setText(itemDesc);
  }

  public void setIconClickListener(OnPhotoIconClickListener iconClickListener) {
    this.iconClickListener = iconClickListener;
  }

  @Override
  public void onClick(View v) {

    iconClickListener.onIconClick(this.getId());
  }

  public String getImgPath() {
    return imgPath;
  }

  public void setNumberIndex(String numberIndex) {
    tvItemIndex.setText(numberIndex);
  }

  public void showIndexView() {

    if (tvItemIndex.getVisibility() != View.VISIBLE) tvItemIndex.setVisibility(View.VISIBLE);
  }

  public interface OnPhotoIconClickListener {

    void onIconClick(int carArchiveItemViewID);
  }
}
